package com.example.foldertreeview.fragments;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.foldertreeview.CallAdapter;
import com.example.foldertreeview.CallRecordPath;
import com.example.foldertreeview.FileTools;
import com.example.foldertreeview.R;
import com.example.foldertreeview.SharedPreferencesUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

//所有fragment的父类，其中提取了很多冗余代码
public class CallShowFragment extends Fragment {
    //列表
    private RecyclerView recyclerView;
    //数据源
    private List<String> callItemList = new ArrayList<>();
    //下拉刷新
    private SwipeRefreshLayout topSwipeRefreshLayout;
    //适配器
    private CallAdapter adapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_call_list, container, false);
        recyclerView = view.findViewById(R.id.recyclerView);
        topSwipeRefreshLayout = view.findViewById(R.id.topSwipeRefreshLayout);

        //设置刷新球颜色
        topSwipeRefreshLayout.setColorSchemeColors(Color.BLUE, Color.RED, Color.YELLOW);
        topSwipeRefreshLayout.setProgressBackgroundColorSchemeColor(Color.parseColor("#BBFFFF"));
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));

        adapter = new CallAdapter(getContext() , callItemList);
        recyclerView.setAdapter(adapter);
        return view;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        //初始化数据源
        loadData(callItemList);

        //设置下拉刷新的监听事件
        topSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //刷新数据源
                loadData(callItemList);

            }
        });
    }

    //调用网络接口，拿到数据源
    public void loadData(List<String> list){
        //清空
        list.clear();
        //列出所有 电话录音
        String path ;
        if(SharedPreferencesUtils.getCallRecordPath(getContext())=="none"){
            path = CallRecordPath.getFilePath();
            Log.e(">>>>>1", path+"");
        }else{
            path = SharedPreferencesUtils.getCallRecordPath(getContext());
            Log.e(">>>>>2", path+"");
        }
        List<File>  fileList = FileTools.searchFiles(new File(path));
        Log.e(">>>>>3", fileList.toString()+"");

        for (File f : fileList) {
            list.add(f.getName());
        }
        adapter.notifyDataSetChanged();
        topSwipeRefreshLayout.setRefreshing(false);
    }
}
